home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / RCS / Semaphore.cc,v < prev    next >
Text File  |  1989-05-04  |  3KB  |  222 lines

  1. head     3.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    grunwald:3.2; strict;
  6. comment  @@;
  7.  
  8.  
  9. 3.2
  10. date     89.02.20.15.37.11;  author grunwald;  state Exp;
  11. branches ;
  12. next     3.1;
  13.  
  14. 3.1
  15. date     88.12.20.13.49.08;  author grunwald;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     88.10.30.13.03.20;  author grunwald;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     88.09.18.16.42.29;  author grunwald;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @@
  32.  
  33.  
  34. 3.2
  35. log
  36. @Start using Gnu library heaps for schedulers
  37. @
  38. text
  39. @// This may look like C code, but it is really -*- C++ -*-
  40. // 
  41. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  42. //
  43. // written by Dirk Grunwald (grunwald@@cs.uiuc.edu)
  44. //
  45. #include "CpuMultiplexor.h"
  46. #include "Semaphore.h"
  47. #include "FifoScheduler.h"
  48. #include "Thread.h"
  49.  
  50. Semaphore::Semaphore(int count, ThreadContainer *scheduler, bool debug)
  51.     : (debug)
  52. {
  53.     if (scheduler == 0) {
  54.     pScheduler = new FifoScheduler;
  55.     } else {
  56.     pScheduler = scheduler;
  57.     }
  58.     pCount = count;
  59. };
  60.  
  61. Semaphore::~Semaphore()
  62. {
  63.     lock.reserve();
  64.  
  65.     if (pScheduler != 0) {
  66.     if (! pScheduler  -> isEmpty() ) {
  67.         cerr << "[Semaphore] Attempted to delete non-empty semaphore\n";
  68.         exit(1);
  69.     }
  70.     delete pScheduler;
  71.     pScheduler = 0;
  72.     }
  73.  
  74.     lock.release();
  75. }
  76.  
  77. void Semaphore::reserve()
  78. {
  79.     lock.reserve();
  80.     if (pCount < 1) {
  81.     lock.release();
  82.     ThisCpu -> reserveByException( this );
  83.     }
  84.     else {
  85.     pCount--;
  86.     lock.release();
  87.     }
  88. }
  89.  
  90. //
  91. //    This is only executed in the context of a CpuMultiplexor, never
  92. //    a thread.
  93. //
  94. bool
  95. Semaphore::reserveByException(Thread *byWho)
  96. {
  97.     bool blocked = 0;
  98.     lock.reserve();
  99.     if (pCount < 1) {
  100.     pScheduler -> add( byWho );
  101.     blocked = 1;
  102.     }
  103.     pCount--;
  104.     lock.release();
  105.     return( blocked );
  106. }
  107.  
  108. bool Semaphore::reserveNoBlock()
  109. {
  110.     bool gotIt = 1;
  111.  
  112.     lock.reserve();
  113.     if (pCount < 1) {
  114.     gotIt = 0;
  115.     }
  116.     else {
  117.     pCount--;
  118.     }
  119.     lock.release();
  120.     return( gotIt );
  121. }
  122.     
  123. void Semaphore::release()
  124. {
  125.     Thread *p = 0;
  126.     lock.reserve();
  127.     if (pCount < 0) {
  128.     assert( !pScheduler -> isEmpty() );
  129.     p = pScheduler -> remove();
  130.     }
  131.     pCount++;
  132.     lock.release();
  133.  
  134.     if (p != 0) {
  135.     ThisCpu -> add(p);
  136.     }
  137. }
  138.  
  139. //
  140. //    Need to lock these
  141. //
  142.  
  143. unsigned Semaphore::size()
  144. {
  145.     return(pScheduler -> size());
  146. }
  147.  
  148. int Semaphore::count(int xcount)
  149. {
  150.     lock.reserve();
  151.     int tmp = pCount;
  152.     pCount = xcount;
  153.     lock.release();
  154.     return(tmp);
  155. }
  156.  
  157. int Semaphore::count()
  158. {
  159.     lock.reserve();
  160.     int tmp = pCount;
  161.     lock.release();
  162.     return(tmp);
  163. }
  164.  
  165. void Semaphore::incrCount(int increment)
  166. {
  167.     lock.reserve();
  168.     pCount += increment;
  169.     lock.release();
  170. }
  171.  
  172. void Semaphore::classPrintOn(ostream& out)
  173. {
  174.     int size = pScheduler -> size();
  175.  
  176.     out << " Semaphore with pCount =" << pCount << " and "
  177.     << size << " enqueued tasks\n";
  178.     if (size > 0) {
  179.     out << "Tasks are:\n" << *pScheduler << "\n";
  180.     }
  181. }
  182.     
  183. @
  184.  
  185.  
  186. 3.1
  187. log
  188. @Steay version
  189. @
  190. text
  191. @d112 1
  192. d115 1
  193. d121 11
  194. a131 1
  195.     return(pCount);
  196. @
  197.  
  198.  
  199. 1.2
  200. log
  201. @*** empty log message ***
  202. @
  203. text
  204. @@
  205.  
  206.  
  207. 1.1
  208. log
  209. @Initial revision
  210. @
  211. text
  212. @d1 7
  213. a7 1
  214. #include "HardwareCpu.h"
  215. d53 1
  216. a53 1
  217. //    This is only executed in the context of a HardwareCpu, never
  218. d97 1
  219. a97 1
  220.     ThisCpu -> add(p,0);
  221. @
  222.